// Copyright 2006 Bret Taylor
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

#include <stdarg.h>
#include <stdio.h>
#include <iostream>

#include "log.h"

namespace indie {

const unsigned int Log::kBufferSize = 1024;

Log::Log(bool report_warnings, bool report_errors) :
    report_warnings_(report_warnings), report_errors_(report_errors),
    num_warnings_(0), num_errors_(0) {
}

Log::~Log() {
}

void Log::Error(const FileLocation& location, const char* format, ...) {
    num_errors_++;

    if (report_errors()) {
	char buffer[kBufferSize];
	va_list argument_list;
	va_start(argument_list, format);
	vsnprintf(buffer, kBufferSize, format, argument_list);
	va_end(argument_list);
	HandleError(location, buffer);
    }
}

void Log::Warning(const FileLocation& location, const char* format, ...) {
    num_warnings_++;

    if (report_warnings()) {
	char buffer[kBufferSize];
	va_list argument_list;
	va_start(argument_list, format);
	vsnprintf(buffer, kBufferSize, format, argument_list);
	va_end(argument_list);
	HandleWarning(location, buffer);
    }
}

void Log::HandleError(const FileLocation& location, const string& message) {
    std::cerr << location.path() << ":" << location.start_line()
	      << ": " << message << std::endl;
}

void Log::HandleWarning(const FileLocation& location, const string& message) {
    std::cerr << location.path() << ":" << location.start_line()
	      << ": warning: " << message << std::endl;
}

}
